package com.jsp.servlet;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jsp.bean.VehicleBean;
import com.jsp.service.VehicleService;


public class VehicleServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private VehicleBean vehicleBean=null;
	private VehicleService vehicleService=null;

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
        this.doPost(request, response);
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
		
		response.setCharacterEncoding("UTF-8");
		String mark = request.getParameter("mark");
		if("insert".equals(mark)){
			try {
				insert(request,response);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}else if("delete".equals(mark)){
			try {
				delete(request,response);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}else if("update".equals(mark)){
			try {
				update(request,response);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

}

	private void update(HttpServletRequest request, HttpServletResponse response)throws SQLException, ServletException, IOException {
		// TODO Auto-generated method stub
		String id=request.getParameter("id");
		String plateNumber=request.getParameter("plateNumber");
		String models=request.getParameter("models");
		String productDate=request.getParameter("productDate");
		String driverInformation=request.getParameter("driverInformation");
		String state=request.getParameter("state");
		vehicleService=new VehicleService();
		vehicleBean=vehicleService.update(id, plateNumber, models, productDate, driverInformation,state);
		if(null!=vehicleBean){
			request.getRequestDispatcher("/admin/updatevehicle.jsp").forward(request, response);
		}else{
			request.getRequestDispatcher("/admin/updatevehicle.jsp").forward(request, response);
		}
	}

	private void delete(HttpServletRequest request, HttpServletResponse response)throws SQLException, ServletException, IOException {
		// TODO Auto-generated method stub
		String id=request.getParameter("id");
		String plateNumber=request.getParameter("plateNumber");
		String models=request.getParameter("models");
		String productDate=request.getParameter("productDate");
		String driverInformation=request.getParameter("driverInformation");
		String state=request.getParameter("state");
		vehicleService=new VehicleService();
		vehicleBean=vehicleService.detele(id, plateNumber, models, productDate, driverInformation,state);
		if(null!=vehicleBean){
			request.getRequestDispatcher("/admin/deletevehiclesuccess.jsp").forward(request, response);
		}else{
			request.getRequestDispatcher("/admin/deletevehicleerror.jsp").forward(request, response);
		}
	}

	private void insert(HttpServletRequest request, HttpServletResponse response)throws SQLException, ServletException, IOException {
		// TODO Auto-generated method stub
		
		String plateNumber=request.getParameter("plateNumber");
		String models=request.getParameter("models");
		String productDate=request.getParameter("productDate");
		String driverInformation=request.getParameter("driverInformation");
		String state=request.getParameter("state");
		System.out.println("dddd"+plateNumber);
		vehicleService=new VehicleService();
		vehicleBean=vehicleService.insert(plateNumber, models, productDate, driverInformation,state);
		if(null!=vehicleBean){
			request.getRequestDispatcher("/admin/insertvehiclesuccess.jsp").forward(request, response);
		}else{
			request.getRequestDispatcher("/admin/insertvehicleerror.jsp").forward(request, response);
		}
	}
}
